package com.wzdigit.wms.wms.service.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.basic.WarehouseAttr;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateUpdateListWarehouseAttrRequest;
import com.wzdigit.wms.basic.client.request.basic.CreateUpdateListWarehouseAttrRequest.CreateUpdateWarehouseAttrRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.WarehouseAttrResponse;
import com.wzdigit.wms.common.entity.StatusCode;
import com.wzdigit.wms.basic.mapper.core.basic.WarehouseAttrMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 仓库-属性 服务类
 * @author yangdongjie
 * @WarehouseAttrService.java
 * @2021年1月4日上午11:14:07
 */
@Service
public class WarehouseAttrService {

    @Autowired
    private WarehouseAttrMapper warehouseAttrMapper;

    public <T> PagerInfo<List<WarehouseAttrResponse>> searchWarehouseAttr(
    		Integer whsId, Pager<T> pager) {
    	Page<T> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
        PagerInfo<List<WarehouseAttrResponse>> pagerInfo = new PagerInfo<List<WarehouseAttrResponse>>();
        List<WarehouseAttrResponse> list = warehouseAttrMapper.selectAllByWhsId(whsId);
        pagerInfo.setItems(list);
        pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> createUpdateListWarehouseAttr(CreateUpdateListWarehouseAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	Date date = new Date();
    	
    	// 记录不删除的属性ID
    	List<Integer> noDelIdList = new ArrayList<Integer>();
    	
    	List<CreateUpdateWarehouseAttrRequest> warehouseAttrs = dto.getWarehouseAttrs();
    	
    	// 去重后的大小
    	Long count = warehouseAttrs.stream().map(one -> one.getAttr()).distinct().count();
    	if(count.intValue() != warehouseAttrs.size()) {
    		Shift.fatal(StatusCode.ATTR_DATA_EXIST);
    	}
    	
    	WarehouseAttr warehouseAttr = new WarehouseAttr();
    	warehouseAttr.setWhsId(dto.getWhsId());
		for (CreateUpdateWarehouseAttrRequest one : warehouseAttrs) {
			warehouseAttr.setAttr(one.getAttr());
			warehouseAttr.setValue(one.getValue());
			warehouseAttr.setComments(one.getComments());
			warehouseAttr.setIsEnabled(one.getIsEnabled());
			if (one.getWhsAttrId() == null) {
				warehouseAttr.setCreateUser(loginName);
				warehouseAttr.setCreateTime(date);
				warehouseAttrMapper.insert(warehouseAttr);
			} else {
				warehouseAttr.setWhsAttrId(one.getWhsAttrId());
				warehouseAttr.setModifyUser(loginName);
				warehouseAttr.setModifyTime(date);
				warehouseAttrMapper.updateById(warehouseAttr);
			}
			noDelIdList.add(warehouseAttr.getWhsAttrId());
		}
		
		// 删除属性
		if (noDelIdList.size() > 0) {
			warehouseAttrMapper.deleteByWhsIdAndNotId(dto.getWhsId(), noDelIdList);
		} else {
			warehouseAttrMapper.deleteByWhsId(dto.getWhsId());
		}
    	return new Result<>();
    }

	/**
	 * 根据仓库ID集合，和属性 字典配置的拓展属性（启用）,属性值查询
	 * @param whsIdList
	 * @param commonFlag
	 * @param itemValue
	 * @param value
	 * @return
	 */
	public List<WarehouseAttr> getPicktaskAttrList(List<Integer> whsIdList, Integer commonFlag, String itemValue,String value) {

		//创建类型的条件构造器
		QueryWrapper<WarehouseAttr> wrapper = new QueryWrapper<>();
		wrapper.in("WHS_ID", whsIdList);
		wrapper.eq("IS_ENABLED", commonFlag);
		wrapper.eq("ATTR", itemValue);
		wrapper.eq("VALUE", value);


		List<WarehouseAttr> warehouseAttrList = warehouseAttrMapper.selectList(wrapper);
		return warehouseAttrList;
	}

	/**
	 * 根据仓库ID集合，和属性 字典配置的拓展属性（启用）查询
	 * @param whsIdList
	 * @param itemValue
	 * @param commonFlag
	 * @return
	 */
	public List<WarehouseAttr> getByWhsIdListAndAttr(List<Integer> whsIdList, String itemValue, Integer commonFlag) {

		//创建类型的条件构造器
		QueryWrapper<WarehouseAttr> wrapper = new QueryWrapper<>();
		wrapper.in("WHS_ID", whsIdList);
		wrapper.eq("IS_ENABLED", commonFlag);
		wrapper.eq("ATTR", itemValue);

		List<WarehouseAttr> warehouseAttrList = warehouseAttrMapper.selectList(wrapper);
		return warehouseAttrList;
	}


    
    /*@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public WarehouseAttrResponse createWarehouseAttr(CreateWarehouseAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	WarehouseAttr warehouseAttr = warehouseAttrMapper.selectOneByAttrAndWhsId(dto.getAttr(), dto.getWhsId());
    	if(warehouseAttr != null) {
    		// 属性已存在
    		Shift.fatal(StatusCode.ATTR_DATA_EXIST);
    	}
    	warehouseAttr = new WarehouseAttr();
    	BeanUtil.copyNonNull(dto, warehouseAttr);
    	warehouseAttr.setCreateUser(loginName);
    	warehouseAttr.setCreateTime(new Date());
    	warehouseAttrMapper.insert(warehouseAttr);
    	
    	WarehouseAttrResponse response = new WarehouseAttrResponse();
    	BeanUtil.copyNonNull(warehouseAttr, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public WarehouseAttrResponse updateWarehouseAttr(UpdateWarehouseAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	WarehouseAttr warehouseAttr = warehouseAttrMapper.selectOneByAttrAndWhsId(dto.getAttr(), dto.getWhsId());
    	if(warehouseAttr != null) {
    		if (dto.getWhsAttrId().intValue() != warehouseAttr.getWhsAttrId().intValue()) {
    			// 属性已存在
    			Shift.fatal(StatusCode.ATTR_DATA_EXIST);
			}
    	}
    	warehouseAttr = warehouseAttrMapper.selectByPrimaryKey(dto.getWhsAttrId());
    	BeanUtil.copy(dto, warehouseAttr);
    	warehouseAttr.setModifyUser(loginName);
    	warehouseAttr.setModifyTime(new Date());
    	warehouseAttrMapper.updateById(warehouseAttr);
    	
    	WarehouseAttrResponse response = new WarehouseAttrResponse();
    	BeanUtil.copy(warehouseAttr, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> deleteWarehouseAttr(Integer whsAttrId) {
    	warehouseAttrMapper.deleteByPrimaryKey(whsAttrId);
		return new Result<>();
    }*/
}
